<template>
  <div v-if="message || $slots.default" :class="alertClasses">
    <div class="flex">
      <!-- 图标 -->
      <div v-if="showIcon" class="flex-shrink-0">
        <component :is="icon" class="h-5 w-5" :class="iconClasses" viewBox="0 0 20 20" fill="currentColor" />
      </div>
      
      <!-- 内容 -->
      <div :class="contentClasses">
        <p v-if="message" class="text-sm font-medium">{{ message }}</p>
        <slot />
      </div>
      
      <!-- 关闭按钮 -->
      <div v-if="dismissible" class="flex-shrink-0 ml-auto pl-3">
        <button
          type="button"
          :class="closeButtonClasses"
          @click="handleDismiss"
        >
          <span class="sr-only">关闭</span>
          <svg class="h-4 w-4" fill="currentColor" viewBox="0 0 20 20">
            <path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd" />
          </svg>
        </button>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'

interface Props {
  type?: 'info' | 'success' | 'warning' | 'error'
  message?: string
  dismissible?: boolean
  showIcon?: boolean
  variant?: 'default' | 'outline' | 'filled'
}

const props = withDefaults(defineProps<Props>(), {
  type: 'info',
  dismissible: false,
  showIcon: true,
  variant: 'default'
})

const emit = defineEmits<{
  dismiss: []
}>()

// 样式配置
const typeConfig = computed(() => {
  const configs = {
    info: {
      bg: 'bg-blue-500/10',
      border: 'border-blue-500/30',
      text: 'text-blue-600',
      icon: InfoIcon
    },
    success: {
      bg: 'bg-green-500/10',
      border: 'border-green-500/30',
      text: 'text-green-600',
      icon: SuccessIcon
    },
    warning: {
      bg: 'bg-yellow-500/10',
      border: 'border-yellow-500/30',
      text: 'text-yellow-600',
      icon: WarningIcon
    },
    error: {
      bg: 'bg-red-500/10',
      border: 'border-red-500/30',
      text: 'text-red-600',
      icon: ErrorIcon
    }
  }
  return configs[props.type]
})

const alertClasses = computed(() => {
  const baseClasses = 'border px-4 py-3 rounded-xl'
  const config = typeConfig.value
  
  if (props.variant === 'outline') {
    return `${baseClasses} bg-transparent ${config.border} ${config.text}`
  } else if (props.variant === 'filled') {
    return `${baseClasses} ${config.bg} ${config.border} ${config.text}`
  } else {
    return `${baseClasses} ${config.bg} ${config.border} ${config.text}`
  }
})

const iconClasses = computed(() => {
  return typeConfig.value.text
})

const contentClasses = computed(() => {
  return props.showIcon ? 'ml-3' : ''
})

const closeButtonClasses = computed(() => {
  const config = typeConfig.value
  return `${config.text} hover:${config.text.replace('text-', 'bg-').replace('-600', '-100')} rounded-md p-1 transition-colors duration-200`
})

const icon = computed(() => {
  return typeConfig.value.icon
})

const handleDismiss = () => {
  emit('dismiss')
}

// 图标组件
const InfoIcon = {
  template: `
    <svg viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd" />
    </svg>
  `
}

const SuccessIcon = {
  template: `
    <svg viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
    </svg>
  `
}

const WarningIcon = {
  template: `
    <svg viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
    </svg>
  `
}

const ErrorIcon = {
  template: `
    <svg viewBox="0 0 20 20" fill="currentColor">
      <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd" />
    </svg>
  `
}
</script>
